home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
DDJMAG
/
DDJ9310.ZIP
/
1993-OCT.ZIP
/
EIFFEL.ASC
< prev
next >
Wrap
Text File
|
1993-09-17
|
5KB
|
224 lines
_THE EIFFEL PROGRAMMING LANGUAGE_
by Robert Howard
Example 1:
is_empty : BOOLEAN is
-- return TRUE if LIST is empty
do
Result := ( head = void )
end ; -- is_empty
remove is
-- remove the first list item
require
not_empty: not is_empty
do
head := head.next ;
if head = void
then
tail := void ;
end;
end ; -- remove
Example 2:
item : T is
-- return the head of the list
require
not is_empty
do
Result := head
end ; -- item
Example 3:
local
this_list : MY_LIST[PRINTABLE] ;
pr : PRINTABLE ;
do
-- create the list
!!this_list ;
-- add items to the list (not shown)
...
-- fetch an item from the list
if not this_list.is_empty
then
pr := this_list.item ;
pr.print_self ; -- dynamic binding still works!
end
Example 4:
pt ?= this_list.item ;
if pt /= void
then -- it was a MY_POINT
pt.print_self ;
end ;
EIFFEL SOURCE CODE EXAMPLE
class DRIVER
-- In Eiffel, the top level driver is an object, too.
inherit
BASIC_IO
creation
make
feature {ANY}
make is
-- run this test driver
local
list1, list2 : MY_LIST[PRINTABLE] ;
n1, n2 : MY_NUMBER ; -- a kind of PRINTABLE
p1, p2 : MY_POINT ; -- also a kinf of PRINTABLE
do
-- create the various objects
!!list1 ;
!!list2 ;
!!n1.set( 10 ) ;
!!n2.set( 20 ) ;
!!p1.set( 2, 3 ) ;
!!p2.set( 4, 5 ) ;
list1.add_to_list( n1 ) ;
list1.add_to_list( n2 ) ;
list1.add_to_list( p1 ) ;
list2.add_to_list( n2 ) ; -- objects can be in more than one list
list2.add_to_list( p1 ) ;
list2.add_to_list( p2 ) ;
list2.add_to_list( list1 ) ; -- list 1 is an element of list 2
put_string( "list1:%N" ) ;
list1.print_self ;
put_string( "list2:%N" ) ;
list2.print_self ;
end -- make
end -- DRIVER
deferred class PRINTABLE
-- insures that 'print_self' is implemented
inherit
BASIC_IO
feature {ANY}
print_self is
-- print yourself
deferred
end -- print_self
end -- PRINTABLE
class MY_NUMBER
-- holds and can print an integer
inherit
PRINTABLE
creation
set
feature {ANY}
value : INTEGER ;
set( new_value : INTEGER ) is
-- set this number
do
value := new_value ;
end ; -- make
print_self is
-- print the value
do
put_string( "Number: " ) ;
put_int( value ) ;
put_newline ;
end ; -- print_self
end -- MY_NUMBER
class MY_POINT
-- holds and can print an x,y pair
inherit
PRINTABLE
creation
set
feature {ANY}
x, y : INTEGER ;
set( new_x : INTEGER; new_y : INTEGER ) is
-- set this point
do
x := new_x ;
y := new_y
end ; -- set
print_self is
-- print the value
do
put_string( "Point: " ) ;
put_int( x ) ;
put_char( ',' ) ;
put_int( y ) ;
put_newline ;
end ; -- print_self
end -- MY_POINT
class ELEMENT[T]
-- holds an object reference and a
-- single link to another ELEMENT[T]
creation
set_data
feature {LIST}
data : T ;
next : ELEMENT[T] ;
set_data( new_data : T ) is
-- set data to the new_data
do
data := new_data
end ; -- set_data
set_next( new_next : ELEMENT[T] ) is
-- set next to the element
do
next := new_next
end ; -- set_next
end -- class ELEMENT
class LIST[T]
-- a generic linked list class
feature {ANY}
add_to_list( data : T ) is
-- add to the end of the list
local
new_element : ELEMENT[T] ;
do
!!new_element.set_data( data ) ;
if head = void
then
head := new_element ;
else
tail.set_next( new_element ) ;
end
tail := new_element ;
end ; -- add_to_list
feature {NONE}
head, tail : ELEMENT[T] ;
invariant
tail_next_is_void: tail.next = void ;
tail_void_when_head_void: head = void implies tail = void
end -- LIST
class MY_LIST[T->PRINTABLE]
-- A printable list which holds printable data
inherit
LIST[T]
PRINTABLE
feature {ANY}
print_self is
-- print the list elements
local
el : ELEMENT[T] ;
do
from
el := head
until
el = void
loop
el.data.print_self
el := el.next ;
end ;
end ; -- print_self
end -- MY_LIST